package com.jn.book.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jn.book.entity.CatalogEntity;
import com.jn.book.repository.CatalogRepository;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class CatalogViewModel extends ViewModel {
    private CatalogRepository mRepository;
    private MutableLiveData<CatalogEntity> mCatalogLiveData = new MutableLiveData<>();

    public CatalogViewModel() {
        mRepository = new CatalogRepository();
    }

    public void getData(String key) {
        mRepository.getCatalog(key).subscribe(new Observer<CatalogEntity>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d("asdf", "onSubscribe");
            }

            @Override
            public void onNext(@NonNull CatalogEntity catalog) {
                Log.d("asdf", "onNext: " + catalog.toString());
                mCatalogLiveData.postValue(catalog);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.d("asdf", "onError");
            }

            @Override
            public void onComplete() {
                Log.d("asdf", "onComplete");
            }
        });
    }

    public LiveData<CatalogEntity> getLiveData() {
        return mCatalogLiveData;
    }
}